<?php

class Engine_Core_Collection_Iterator implements Iterator
{
    protected
        $items = array(),
		$index = 0
    ;

    public function rewind()
    {
        $this->index = 0;

		if ($k = array_keys($this->items))
		{
			if(isset($this->items[$k[$this->index]]))
			{
				return $this->items[$k[$this->index]];
			}
		}
    }

    public function current()
    {
        if (!$this->items)
        {
            return null;
        }

		$k = array_keys($this->items);

		return $this->items[$k[$this->index]];
    }

    public function key()
    {
        if (!$this->items)
        {
            return null;
        }

		$k = array_keys($this->items);

		return $k[$this->index];
    }

    public function next()
    {
        $k = array_keys($this->items);

		if (isset($k[++$this->index]))
        {
            return $this->items[$k[$this->index]];
        }

		return null;
    }

    public function valid()
    {
        if (!$this->items)
        {
            return false;
        }

        $k = array_keys($this->items);

		return isset($k[$this->index]);
    }

	public function last()
	{
		$keys = array_keys($this->items);

		return $this->items[end($keys)];
	}

	/**
	 * @return Engine_Core_Wrapper
	 */
	public function first()
	{
		$keys = array_keys($this->items);

		if (!empty($this->items[reset($keys)])) {
			return $this->items[reset($keys)];
		}
	}
}